<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Contact extends CI_Controller {

	public function index(){
            $isLogin = $this->session->userdata('is_login');
            if(isset($isLogin)){
                $recipient=filter_input(INPUT_GET,'recipient',FILTER_SANITIZE_STRING);
                $this->load->model('user');// loading user model
                $recipientUser=$this->user->getUser($recipient);
                $hasSession=$isLogin;
                $uid = $this->session->userdata("user_id");
                $curUser=$this->user->getUser($uid);
                $data['user']=$curUser;
                $data['recipient']=$recipientUser;

            }else{
                $hasSession=FALSE;
            }
            $data['hasSession']=$hasSession;
            $this->load->view('contactpage',$data);
	}
        
        public function notifyUser(){
            $isLogin = $this->session->userdata('is_login');
            if(isset($isLogin)){
                $message=filter_input(INPUT_POST,'message',FILTER_SANITIZE_STRING);
                $subject=  filter_input(INPUT_POST, 'subject',FILTER_SANITIZE_STRING);
                $uid=  filter_input(INPUT_POST, 'recipientId',FILTER_SANITIZE_STRING);
                $this->load->model('Message');// loading user model
                $success=$this->Message->addMessage($uid,$subject,$message,'N');
                if($success){
                    $response=array("success"=>$success,'message'=>"Your messsge has been sent");
                }else{
                    $response=array("success"=>FALSE,"message"=>'Send message failed');
                }
            }else{
                $response=array("success"=>FALSE,"message"=>"Please sign in to send message");
            }
            echo json_encode($response);
        }
}
